package com.jh.paymentchannel.payment.wechat;

import com.jh.paymentchannel.pojo.PaymentOrder;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.TreeMap;

import static io.jsonwebtoken.lang.Strings.hasText;

/**
 * @author Yichuan
 * @version 1.0
 */
public class WechatPayRequest implements Serializable {

    private static final long serialVersionUID = -7587777641538047482L;

    public static WechatPayRequest request(@NotNull WechatConfig wechatConfig,
                                           @NotNull PaymentOrder paymentOrder, String openId) {
        WechatPayRequest request = new WechatPayRequest(wechatConfig.getAppId(), wechatConfig.getMchId(),
                paymentOrder.getBody(), paymentOrder.getOutTradeNo(), paymentOrder.getSpbillCreateIp());
        request.setSub_mch_id(paymentOrder.getSubMchId());
        request.setNonce_str(WechatUtil.generateNonceStr());
        BigDecimal totalFee = paymentOrder.getTotalFee().multiply(BigDecimal.valueOf(100));
        request.setTotal_fee(Integer.toString(totalFee.intValue()));
        request.setTrade_type("JSAPI"); // MWEB
        request.setSign_type("HMAC-SHA256");
        request.setOpenid(openId);
        return request;
    }

    public static TreeMap<String, String> queryParams(WechatConfig wechatConfig, PaymentOrder paymentOrder) {
        TreeMap<String, String> map = new TreeMap<>();
        map.put("appid", wechatConfig.getAppId());
        map.put("mch_id", wechatConfig.getMchId());
        map.put("out_trade_no", paymentOrder.getOutTradeNo());
        map.put("nonce_str", WechatUtil.generateNonceStr());
        map.put("sign_type", "HMAC-SHA256");
        map.put("sub_mch_id", paymentOrder.getSubMchId());
        return map;
    }

    public TreeMap<String, String> dataToMap() {
        TreeMap<String, String> map = new TreeMap<>();
        map.put("appid", getAppid());
        map.put("mch_id", getMch_id());
        if (hasText(getDevice_info())) {
            map.put("device_info", getDevice_info());
        }
        map.put("sub_mch_id", getSub_mch_id());
        map.put("nonce_str", getNonce_str());
        if (hasText(getSign())) {
            map.put("sign", getSign());
        }
        if (hasText(getSign_type())) {
            map.put("sign_type", getSign_type());
        }
        map.put("body", getBody());
        if (hasText(getDetail())) {
            map.put("detail", getDetail());
        }
        if (hasText(getAttach())) {
            map.put("attach", getAttach());
        }
        map.put("out_trade_no", getOut_trade_no());
        if (hasText(getFee_type())) {
            map.put("fee_type", getFee_type());
        }
        map.put("total_fee", getTotal_fee());
        map.put("spbill_create_ip", getSpbill_create_ip());
        if (hasText(getTime_start())) {
            map.put("time_start", getTime_start());
        }
        map.put("notify_url", getNotify_url());
        map.put("trade_type", getTrade_type());
        map.put("openid", getOpenid());
        return map;
    }

    // 微信账号ID
    private String appid;
    // 商户号
    private String mch_id;
    // 子商户号
    private String sub_mch_id;
    // 设备号(非)
    private String device_info;
    // 随机字符串
    private String nonce_str;
    // 签名
    private String sign;
    // 签名类型(非)
    private String sign_type;
    // 商品描述
    private String body;
    // 商品详情(非)
    private String detail;
    // 附加数据(非)
    private String attach;
    // 商户订单号
    private String out_trade_no;
    // 标价币种，默认人民币(非)
    private String fee_type;
    // 金额(单位：分)
    private String total_fee;
    // 终端IP
    private String spbill_create_ip="127.0.0.1";
    // 交易起始时间，格式为yyyyMMddHHmmss(非)
    private String time_start;
    // 异步回调地址
    private String notify_url;
    // 交易类型(JSAPI--公众号支付、NATIVE--原生扫码支付、APP--app支付)
    private String trade_type;
    // 用户标识
    private String openid;

    public WechatPayRequest() {
        super();
    }

    public WechatPayRequest(String appid, String mch_id,
                            String body, String out_trade_no,
                            String spbill_create_ip) {
        this.appid = appid;
        this.mch_id = mch_id;
        this.body = body;
        this.out_trade_no = out_trade_no;
        this.spbill_create_ip = spbill_create_ip;
    }

    public String getNotify_url() {
        return notify_url;
    }

    public void setNotify_url(String notify_url) {
        this.notify_url = notify_url;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getMch_id() {
        return mch_id;
    }

    public void setMch_id(String mch_id) {
        this.mch_id = mch_id;
    }

    public String getDevice_info() {
        return device_info;
    }

    public void setDevice_info(String device_info) {
        this.device_info = device_info;
    }

    public String getNonce_str() {
        return nonce_str;
    }

    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getSign_type() {
        return sign_type;
    }

    public void setSign_type(String sign_type) {
        this.sign_type = sign_type;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public String getAttach() {
        return attach;
    }

    public void setAttach(String attach) {
        this.attach = attach;
    }

    public String getOut_trade_no() {
        return out_trade_no;
    }

    public void setOut_trade_no(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }

    public String getFee_type() {
        return fee_type;
    }

    public void setFee_type(String fee_type) {
        this.fee_type = fee_type;
    }

    public String getTotal_fee() {
        return total_fee;
    }

    public void setTotal_fee(String total_fee) {
        this.total_fee = total_fee;
    }

    public String getSpbill_create_ip() {
        return spbill_create_ip;
    }

    public void setSpbill_create_ip(String spbill_create_ip) {
        this.spbill_create_ip = spbill_create_ip;
    }

    public String getTime_start() {
        return time_start;
    }

    public void setTime_start(String time_start) {
        this.time_start = time_start;
    }

    public String getTrade_type() {
        return trade_type;
    }

    public void setTrade_type(String trade_type) {
        this.trade_type = trade_type;
    }

    public String getSub_mch_id() {
        return sub_mch_id;
    }

    public void setSub_mch_id(String sub_mch_id) {
        this.sub_mch_id = sub_mch_id;
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }
}
